package com.littcore.saas.rest.personal;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.littcore.common.BeanManager;
import com.littcore.saap.common.vo.LoginUserVo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.message.biz.IUserMessageBizService;
import com.littcore.saap.message.bo.UserMessageBo;
import com.littcore.saap.message.vo.UserMessageStatVo;
import com.littcore.shield.vo.ILoginVo;

/**
 * .
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年12月7日
 * @version 1.0
 */
@Api(name = "我的消息接口", description = "个人消息通知")
@Controller
public class MyMessageServiceController {
  
  @Resource IUserMessageBizService userMessageBizService;
  
  @ApiMethod(description = "设置指定消息为已读")
  @RequestMapping
  public void read(@ApiQueryParam @RequestParam Integer id)
  {
    userMessageBizService.doRead(id);
  }

  @ApiMethod(description = "删除指定消息")
  @RequestMapping
  public void delete(@ApiQueryParam @RequestParam Integer id)
  {
    userMessageBizService.delete(id);
  }

  @ApiMethod(description = "设置所有未读消息为已读")
  @RequestMapping
  public void readAll(@ApiQueryParam @RequestParam(required=false) String moduleCode)
  {
    LoginUserVo loginUser = (LoginUserVo)LoginUtils.getLoginVo();
    
    userMessageBizService.doReadAll(loginUser.getOpId().intValue(), loginUser.getTenantId(), moduleCode);
  }

  @ApiMethod(description = "删除所有消息")
  @RequestMapping
  public void deleteAll(@ApiQueryParam @RequestParam(required=false) String moduleCode)
  {
    LoginUserVo loginUser = (LoginUserVo)LoginUtils.getLoginVo();
    
    userMessageBizService.deleteAll(loginUser.getOpId().intValue(), loginUser.getTenantId(), moduleCode);
  }

  /**
   * 获得当前登录用户未读消息统计.
   * @return
   */
  @ApiMethod(description = "获得未读消息数量")
  @RequestMapping
  public ModelAndView getUnreadCount()
  {
    ILoginVo loginUser = LoginUtils.getLoginVo();
    int tenantId = LoginUtils.getTenantId();
    Locale locale = loginUser.toLocale();
    
    List<UserMessageStatVo> userMessageStats = userMessageBizService.countUnread(loginUser.getOpId().intValue(), tenantId);
    int totalCount = 0;
    for (UserMessageStatVo vo : userMessageStats)
    {
      totalCount += vo.getUnreadCount();
      
      String moduleName = BeanManager.getMessage("message.module."+vo.getModuleCode(), locale);
      String title = BeanManager.getMessage("userMessageStat.title", new Object[]{vo.getUnreadCount(), moduleName}, locale);
      vo.addI18n("moduleName", moduleName);
      vo.addI18n("title", title);
    }
    
    return new ModelAndView("jsonView").addObject("userMessageStats", userMessageStats).addObject("totalCount", totalCount);
  }

  /**
   * 获得所有未读消息.
   *
   * @param moduleCode the module code
   * @return the unread messages
   */
  @ApiMethod(description = "获得未读消息记录")
  @RequestMapping
  public ModelAndView getUnreadMessages(@RequestParam(required=false) String moduleCode)
  {
    ILoginVo loginUser = LoginUtils.getLoginVo();
    int tenantId = LoginUtils.getTenantId();
    Locale locale = loginUser.toLocale();
    
    List<UserMessageBo> unreadMessages = userMessageBizService.findBy(loginUser.getOpId().intValue(), tenantId, false, moduleCode);
    List<String[]> moduleNames = new ArrayList<String[]>();
    for (final UserMessageBo bo : unreadMessages)
    {     
      boolean isExist = CollectionUtils.exists(moduleNames, new Predicate() {
        
        @Override
        public boolean evaluate(Object object)
        {
          String[] array = (String[]) object;
          return bo.getModuleCode().equals(array[0]);
        }
      });
      if(!isExist)
      {
        moduleNames.add(new String[]{bo.getModuleCode(), bo.getI18n("moduleName")});      
      }
    }  
    return new ModelAndView("jsonView").addObject("unreadMessages", unreadMessages).addObject("moduleNames", moduleNames);
  }

  /**
   * 获得所有已读消息.
   *
   * @return the read messages
   */
  @ApiMethod(description = "获得已读消息记录")
  @RequestMapping
  public ModelAndView getReadMessages(@RequestParam(required=false) String moduleCode)
  {
    ILoginVo loginUser = LoginUtils.getLoginVo();
    int tenantId = LoginUtils.getTenantId();
    Locale locale = loginUser.toLocale();
    
    List<UserMessageBo> readMessages = userMessageBizService.findBy(loginUser.getOpId().intValue(), tenantId, true, moduleCode);
    List<String[]> moduleNames = new ArrayList<String[]>();
    for (final UserMessageBo bo : readMessages)
    {     
      boolean isExist = CollectionUtils.exists(moduleNames, new Predicate() {
        
        @Override
        public boolean evaluate(Object object)
        {
          String[] array = (String[]) object;
          return bo.getModuleCode().equals(array[0]);
        }
      });
      if(!isExist)
      {
        moduleNames.add(new String[]{bo.getModuleCode(), bo.getI18n("moduleName")});      
      }
    }  
    return new ModelAndView("jsonView").addObject("readMessages", readMessages).addObject("moduleNames", moduleNames);
  }

}